package sfsu.memopad;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/*
 * Map browser that allows the user to select a location on the map and have the
 * information passed back to the edit screen.
 */
public class MapActivity extends com.google.android.maps.MapActivity {
	private String myAddress = "";
	private int maxResults = 3;
	private List<Address> addresses; // a list to hold the addresses
	private List<GeoPoint> geoPoints = new ArrayList<GeoPoint>();
	private Geocoder gc;

	private double latitude = 0;
	private double longitude = 0;
	private Location location;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.map);
		MapView mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);

		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable pin = this.getResources().getDrawable(R.drawable.pushpin);
		HelloItemizedOverlay itemizedOverlay = new HelloItemizedOverlay(pin, this);

		gc = new Geocoder(this);

		// getFromLocationName() is not testable on the emulator so commenting
		// out this code
		// this code should work on the actual phone
		/*
		 * try{ addresses = gc.getFromLocationName(myAddress, maxResults); int
		 * nextAddress = 0; if(addresses != null) { while(nextAddress <
		 * maxResults){ Address x = addresses.get(nextAddress); //get an address
		 * to retrieve lat and lont geoPoints.add(new
		 * GeoPoint((int)(x.getLatitude()*1e6), (int)(x.getLongitude()*1e6)));
		 * nextAddress++; } } } catch (IOException e){ }
		 */
		
		geoPoints.add(new GeoPoint((int) (37.722053 * 1e6),	(int) (-122.480092 * 1e6)));
		geoPoints.add(new GeoPoint((int) (37.720848 * 1e6),	(int) (-122.476852 * 1e6)));
		geoPoints.add(new GeoPoint((int) (37.768272 * 1e6),	(int) (-122.479362 * 1e6)));

		// For emulator testing only.  Hard coding address.
		myAddress = "SFSU";
		latitude = geoPoints.get(0).getLatitudeE6() / 1e6;
		longitude = geoPoints.get(0).getLongitudeE6() / 1e6;
		
		/**
		 * MapController is needed to set view location and zooming.
		 */
		int nextPoint = 0;
		while (nextPoint < geoPoints.size()) {
			OverlayItem overlayitem = new OverlayItem(geoPoints.get(nextPoint),	"SFSU", "I'm in SFSU!");
			itemizedOverlay.addOverlay(overlayitem);
			mapOverlays.add(itemizedOverlay);

			MapController mc = mapView.getController();
			mc.setZoom(14);
			mc.setCenter(geoPoints.get(nextPoint));
			nextPoint++;
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	protected void storeMapData() {
		Intent intent = new Intent();
		Log.d("Map", "Address: " + myAddress);
		Log.d("Map", "log=" + longitude + ", lat=" + latitude);
		
		intent.putExtra("LATITUDE", latitude);
		intent.putExtra("LONGITUDE", longitude);
		// intent.putExtra("ADDRESS", reverseGeoLocation(location));
		intent.putExtra("ADDRESS", myAddress);
		setResult(RESULT_OK, intent);
	}

	/*
	 * Reverse geolocation using given location parameter resulting in actual
	 * address.
	 */
	private String reverseGeoLocation(Location location) {
		List<Address> addresses;
		StringBuilder label = new StringBuilder("");
		try {
			Geocoder geoCoder = new Geocoder(this, Locale.ENGLISH);
			addresses = geoCoder.getFromLocation(location.getLatitude(),
					location.getLongitude(), 1);

			if (addresses != null) {
				Address currentAddress = addresses.get(0);
				for (int x = 0; x < currentAddress.getMaxAddressLineIndex(); x++) {
					label.append(currentAddress.getAddressLine(x)).append(", ");
				}
				label.deleteCharAt(label.length() - 1);
				label.deleteCharAt(label.length() - 1);
				// label.append("\nLat: " + location.getLatitude() + "\nLng: " +
				// location.getLongitude());
				// textBox.setText(label.toString());
			}
		} catch (IOException e) {
			// textBox.setText("Error in reverse geolocation: " +
			// e.getMessage());
			Log.e("MapActivity",
					"Error in reverse geolocation: " + e.getMessage());
		}
		return label.toString();
	}

	@Override
	public void onBackPressed() {
		storeMapData();
		finish();
	}
}